<?php

/**
 *    卖家限时促销控制器
 *
 *    @author    yanghj
 *    @date      2015.04.11
 */
class PromotionApp extends BackendApp
{
    var $_activity_mod;
    var $_activity_goods_mod;
    var $_goods_mod;
    var $_store_mod;

    /* 构造函数 */

    function __construct()
    {
        $this->PromotionApp();
    }

    function PromotionApp()
    {
        parent::__construct();

        $this->_activity_mod = & m('activity');
        $this->_activity_goods_mod = & m('activitygoods');
        $this->_goods_mod = & m('goods');
        $this->_store_mod = & m('store');
    }
    
    /**
     *    参与活动的商品
     *
     */   
    function index()
    {
        /* 取得列表数据 */
        $sql = "SELECT a.*,s.store_name FROM {$this->_activity_mod->table} AS a left join {$this->_store_mod->table} AS s on a.store_id = s.store_id WHERE a.act_type = 1";
        $activity = $this->_activity_mod->getAll($sql);
        $temp = array();
        $store_list = array();
        $act_title_list = array();
        if($activity)
        {
            foreach($activity as $key=>$val)
            {
                $activity[$key]['validity'] = ceil(($activity[$key]['end_time'] - $activity[$key]['start_time'])/3600);
                if(time() < $activity[$key]['start_time'])
                {
                    $activity[$key]['act_status'] = '未开始';
                }
                if(time() >= $activity[$key]['start_time'] &&  time() <= $activity[$key]['end_time'])
                {
                    $activity[$key]['act_status'] = '已开始';
                }
                if(time() > $activity[$key]['end_time'])
                {
                    $activity[$key]['act_status'] = '已结束';
                }
                $temp[$val['act_id']] = $activity[$key];
                $store_list[$val['store_id']] = $val['store_name'];
                $act_title_list[$val['act_name']] = $val['act_name'];
            }

        }


        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'activity.act_name',
                'name'  => 'act_name',
                'equal' => '=',
            ),
            array(
                'field' => 'g.goods_name',
                'name'  => 'goods_name',
                'equal' => 'like',
            ),
            array(
                'field' => 'activity_goods.goods_id',
                'name'  => 'goods_id',
                'type' => 'int',
            ),
            array(
                'field' => 'activity.store_id',
                'name'  => 'store_id',
                'type' => 'int',
            ),
        ));

        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc')))
            {
                $sort = 'this.act_id';
                $order = 'desc';
            }
        }
        else
        {
            $sort = 'this.act_id';
            $order = 'desc';
        }

        $page = $this->_get_page();
        $activity_list = $this->_activity_goods_mod->find(array(
            'conditions' => "1 " . $conditions,
            'join' => 'belongs_to_activity, belongs_to_goods',
            'fields' => 'this.*, g.goods_name, g.price, g.promote_price as goods_promote_price',
            'count' => true,
            'order' => "$sort $order",
            'limit' => $page['limit'],
        ));

        if($activity_list)
        {
            foreach($activity_list as $key=>$val)
            {
                $activity_list[$key]['store_name'] = $temp[$val['act_id']]['store_name'];
                $activity_list[$key]['act_name'] = $temp[$val['act_id']]['act_name'];
                $activity_list[$key]['validity'] = $temp[$val['act_id']]['validity'];
                $activity_list[$key]['start_time'] = $temp[$val['act_id']]['start_time'];
                $activity_list[$key]['end_time'] = $temp[$val['act_id']]['end_time'];
                $activity_list[$key]['act_status'] = $temp[$val['act_id']]['act_status'];
                $activity_list[$key]['free_shipping'] = $temp[$val['act_id']]['free_shipping'] == 1 ? '包邮' : '--';
                $activity_list[$key]['promote_discount'] = $activity_list[$key]['promote_discount'] > 0 ? $activity_list[$key]['promote_discount']*10 : '--';
                $activity_list[$key]['promote_price'] = $activity_list[$key]['promote_price'] > 0 ? $activity_list[$key]['promote_price'] : '--';
            }
        }

        $page['item_count'] = $this->_activity_goods_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        $this->assign('activity_list', $activity_list);
        $this->assign('activity_category', $temp);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('store_list', $store_list);
        $this->assign('act_title_list', $act_title_list);
        $this->display('promotion.index.html');
    }


    /**
     *    添加活动闪爆商品
     *
     */
    function add()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$id)
        {
            $this->show_warning("该商品不存在");

            return;
        }
        if (!IS_POST)
        {
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('promotion.form.html');
        }
        else
        {
            $data = array();
            $data['goods_img'] =   $this->_upload_img();
            if ($data['goods_img'] === false)
            {
                $this->show_warning('上传图片失败');
                return;
            }
            $data['goods_title']  =   html_script($_POST['goods_title']);
            $data['goods_desc']   =   html_script($_POST['goods_desc']);
            $data['total_number']   =   intval($_POST['total_number']);

            $this->_activity_goods_mod->edit($id, $data);
            if ($this->_activity_goods_mod->has_error())    //有错误
            {
                $this->show_warning($this->_activity_goods_mod->get_error());

                return;
            }

            $this->admin_log('设为万菱闪购商品:'.$id);
            $this->show_message('设为万菱闪购商品成功',
                'back_list',     'index.php?app=promotion');
        }
    }
    
    /**
     *    编辑活动闪爆商品
     *
     */
    function edit()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$id)
        {
            $this->show_warning("该商品不存在");

            return;
        }

        $promotion     = $this->_activity_goods_mod->get_info($id);

        if (empty($promotion))
        {
            $this->show_warning("该商品不存在");
            return;
        }

        if (!IS_POST)
        {
            $this->assign('promotion', $promotion);
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('promotion.form.html');
        }
        else
        {
            $data = array();
            $goods_img =   $this->_upload_img();
            $data['goods_img'] = !empty($goods_img) ? $goods_img : $promotion['goods_img'];
            if (empty($data['goods_img']))
            {
                $this->show_warning('上传图片失败');
                return;
            }
            $data['goods_title']  =   html_script($_POST['goods_title']);
            $data['goods_desc']   =   html_script($_POST['goods_desc']);
            $data['total_number']   =   intval($_POST['total_number']);

            $this->_activity_goods_mod->edit($id, $data);
            if ($this->_activity_goods_mod->has_error())    //有错误
            {
                $this->show_warning($this->_activity_goods_mod->get_error());

                return;
            }

            import('img_server');
            if ($goods_img && $promotion['goods_img'])
            {
                $imageUrl = remove_image_server_host($promotion['goods_img']);
                @unlink(ROOT_PATH . '/' . $imageUrl);  //删除图片文件
            }

            $this->admin_log('修改万菱闪购商品:'.$id);
            $this->show_message('修改万菱闪购商品成功',
                'back_list',     'index.php?app=promotion');
        }

    }
    
    /**
     *    删除活动闪爆商品
     *
     */
    function drop()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$id)
        {
            $this->show_warning("该商品不存在");

            return;
        }
        $promotion     = $this->_activity_goods_mod->get_info($id);

        if (empty($promotion))
        {
            $this->show_warning("该商品不存在");
            return;
        }

        $data = array();
        $data['goods_img'] = '';
        $data['goods_title'] =  '';
        $data['goods_desc']  =  '';

        $this->_activity_goods_mod->edit($id, $data);
        if ($this->_activity_goods_mod->has_error())    //有错误
        {
            $this->show_warning($this->_activity_goods_mod->get_error());

            return;
        }

        import('img_server');
        if ( $promotion['goods_img'])
        {
            $imageUrl = remove_image_server_host($promotion['goods_img']);
            @unlink(ROOT_PATH . '/' . $imageUrl);  //删除图片文件
        }

        $this->admin_log('取消万菱闪购商品:'.$id);
        $this->show_message('取消万菱闪购商品成功');
    }

    //异步修改数据
    function ajax_col()
    {
        $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data   = array();
        if (in_array($column ,array('sort_order')))
        {
            $data[$column] = $value;
            $this->_activity_goods_mod->edit($id, $data);
            if(!$this->_activity_goods_mod->has_error())
            {
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return ;
        }
        return ;
    }

    /**
     *    处理上传标志
     *
     *    @author    Garbin
     *    @param     int $adv_id
     *    @return    string
     */
    function _upload_img()
    {
        $file = $_FILES['goods_img'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
        {
            return '';
        }
        import('uploader.lib');             //导入上传类
        import('img_server');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['goods_img']);//上传图片

        if (!$uploader->file_info())
        {
            $this->show_warning($uploader->get_error());
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);
        /* 上传 */
        if ($file_path = $uploader->save('data/files/mall/promotion', $uploader->random_filename()))   //保存到指定目录
        {
            return get_image_server_url($file_path);
        }
        else
        {
            return false;
        }
    }

}

?>